import json
import logging
import sys

import requests

from comm.dateTime import nowData
from comm.dbConnect import dbJonOne
from setting import openDbBase

class u9cApiList:
    dbproconfig = openDbBase()
    # 接口授权秘钥
    # clientsecret = "50a2b4e9c2d546b686c5cf9b25ae605a"
    clientsecret = dbproconfig['u9capi']['clientsecret']
    # 接口授权Id
    # clientid = "WMS"
    clientid = dbproconfig['u9capi']['clientid']
    # 企业编码
    # entCode = "01"
    entCode = dbproconfig['u9capi']['entCode']
    # 组织编码(接口授权Id)
    # orgCode = "100"
    orgCode = dbproconfig['u9capi']['orgCode']
    # apiAdd = "http://172.18.160.10/U9C/webapi/"
    apiAdd = dbproconfig['u9capi']['apiAdd']
    userCode = "wms"
    # userCode = dbproconfig['u9capi']['userCode']

    @classmethod
    def getToken(cls):
        import os
        # 当前脚本作为主程序运行时的路径
        if getattr(sys, 'frozen', False):
            entry_point_path = sys.executable
            # print(111,entry_point_path)
        else:
            entry_point_path = sys.modules['__main__'].__file__
            # print(222,entry_point_path)
        entry_point_abs_path = os.path.dirname(os.path.abspath(entry_point_path))
        # print("入口文件路径:", entry_point_abs_path)
        entry_point_abs_path = entry_point_abs_path + '\\api__init__.txt'
        if os.path.exists(entry_point_abs_path):
            # 文件存在读取文件中的wms token
            with open(entry_point_abs_path, 'r', encoding="utf-8") as file:
                content = file.read()
                try:
                    apiTokenObj = json.loads(content)
                except Exception as e:
                    logging.info('wms读取缓存中的token失败' + repr(e))
                    return None, "wms读取缓存中的token失败" + repr(e)
                # 文件中不存在wms的token则登录
                if "u9c" not in apiTokenObj.keys():
                    # print("不存在wms的token")
                    logging.info('不存在u9c的token')
                    token, err = cls.login()
                    if err != None:
                        return None, err
                    getToken = {}
                    getToken["token"] = token
                    getToken["time"] = nowData(hour=1)
                    apiTokenObj["u9c"] = getToken
                    # 将token写入到文件中
                    with open(entry_point_abs_path, 'w', encoding="utf-8") as file:
                        file.write(json.dumps(apiTokenObj))
                # 判断token是否过期
                print(apiTokenObj['u9c']["time"],"===", nowData())
                if apiTokenObj['u9c']["time"] <= nowData():
                    logging.info('u9c的token过期重新登录获取')
                    # token过期重新获取token
                    # print("wms的token过期重新获取token")
                    token, err = cls.login()
                    if err != None:
                        return None, err
                    with open(entry_point_abs_path, 'w', encoding="utf-8") as file:
                        getToken = {}
                        getToken["token"] = token
                        getToken["time"] = nowData(hour=1)
                        apiTokenObj["u9c"] = getToken
                        # 将token写入到文件中
                        print(apiTokenObj)
                        file.write(json.dumps(apiTokenObj))
                token = apiTokenObj['u9c']["token"]
        else:
            # print("文件不存在")
            logging.info('u9c的token文件不存在登录获取token')
            token, err = cls.login()
            if err != None:
                return None, err
            with open(entry_point_abs_path, 'w', encoding="utf-8") as file:
                getToken = {}
                getToken["token"] = token
                getToken["time"] = nowData(hour=1)
                apiTokenObj = {}
                apiTokenObj["u9c"] = getToken
                # 将token写入到文件中
                file.write(json.dumps(apiTokenObj))
        return token, None
    @classmethod
    def login(cls):
        url = cls.apiAdd + "OAuth2/AuthLogin"
        # url = cls.apiAdd + "OAuth2/GetAuthorizeCode"
        header = {
            "Content-Type":"application/json"
        }
        params = {}
        params['clientsecret'] = cls.clientsecret
        params['clientid'] = cls.clientid
        params['entCode'] = cls.entCode
        params['orgCode'] = cls.orgCode
        params['userCode'] = cls.userCode
        # print(url)
        r = requests.get(url,params=params,headers=header)
        if r.status_code != 200:
            # print(r.json())
            # print(r.status_code)
            return None, 'api接口登录请求失败'
        # print(r.status_code)
        resData = r.json()
        # print(resData)
        if resData['ResCode']!=0:
            return None,"账号不存在或密码错误 "+ str(resData['ResCode']) + resData['ResMsg']
        return resData['Data'],None

    @classmethod
    def orgCodeConfig(cls,db):
        sql1 = "select id,code from Base_Organization where code='{0}'".format(cls.orgCode)
        data, err = dbJonOne(sql=sql1, engineName=None, parm={}, db_session=db)
        if err!=None:
            return None, err
        if data is None:
            return None,'未查询到组织编码'
        return data['id'], None
    # 新增工序转移单
    @classmethod
    def yaZhuDepAdd(cls,data,token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token":token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '新增失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 审核工序转移单
    @classmethod
    def yaZhuDepSh(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '审核接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '审核失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 删除工序转移单
    @classmethod
    def yaZhuDepDelete(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Delete"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '删除失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '删除失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 生成生产订单
    @classmethod
    def yaZhuDepMoAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增生成订单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增生成订单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '新增生成订单失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 审核生产订单
    @classmethod
    def yaZhuDepMoSh(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/ApproveMo"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '审核生成订单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '审核生成订单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '审核生成订单失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 生产订单开工
    @classmethod
    def yaZhuDepMoStar(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/StartMo"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生产订单开工接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '生产订单开工失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生产订单开工失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 生产完工报告单
    @classmethod
    def yaZhuDepMoEndr(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "CompleteDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生成订单完工报告接口请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '生成订单完工报告失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生成订单完工报告失败' + resData['Data'][0]['m_errorMsg']
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['no'] = resData['Data'][0]['m_code']
        # print(data,'生成完工报告单')
        return data, None

    # 修改完工报告单
    @classmethod
    def yaZhuDepMoEndrUpdate(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "CompleteDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生成订单完工报告接口请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '生成订单完工报告失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生成订单完工报告失败' + resData['Data'][0]['m_errorMsg']
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['no'] = resData['Data'][0]['m_code']
        # print(data, '生成完工报告单')
        return data, None

    # 完工报告单提交跟审核
    @classmethod
    def yaZhuDepMoEndrShenHe(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # # 完工报告单提交
        # url = cls.apiAdd + "CompleteDoc/Approve"
        # jsonData = data
        # # pprint(jsonData)
        # # print(json.dumps(jsonData,ensure_ascii=False))
        # header = {
        #     "Content-Type": "application/json",
        #     "token": token
        # }
        # r = requests.post(url, json=jsonData, headers=header)
        # if r.status_code != 200:
        #     return None, '完工报告单提交接口请求失败'
        # resData = r.json()
        # print(resData)
        # if resData['Success'] != True:
        #     return None, '完工报告单提交失败' + resData['ResMsg']
        # if resData['Success'] == True:
        #     if resData['Data'][0]['m_isSucess'] == False:
        #         return None, '完工报告单提交失败' + resData['Data'][0]['m_errorMsg']

        # 完工报告单审核
        url = cls.apiAdd + "CompleteDoc/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '完工报告单审核接口请求失败'
        resData = r.json()
        print(resData)
        if resData['Success'] != True:
            return None, '完工报告单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '完工报告单审核失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    # 成品入库单修改
    def chengPingUpdate(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 成品入库单审核
        url = cls.apiAdd + "RcvRptDoc/Modify"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '成品入库单修改接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '成品入库单修改失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '成品入库单修改失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    @classmethod
    # 成品入库单审核
    def chengPingShenHe(cls, code):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 成品入库单审核
        url = cls.apiAdd + "RcvRptDoc/Approve"
        jsonData = [
              {
                    "DocNo": code,
                    "IsSuccess": True,
                    "ErrorInfo": "",
                    "CUD": 0
              }
        ]
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '成品入库单审核接口请求失败'
        resData = r.json()
        if resData['Success'] != True:
            return None, '成品入库单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '成品入库单审核失败' + resData['Data'][0]['m_errorMsg']
        return resData, None

    # 生成调入单
    @classmethod
    def diaoRuAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "TransferIn/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增生成调入单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增生成调入单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '新增生成调入单失败' + resData['Data'][0]['ErrorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['ID']
        data['no'] = resData['Data'][0]['Code']
        return data, None
    # 调入单删除
    @classmethod
    def diaoRuDelete(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "TransferIn/Delete"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除调入单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '删除调入单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '删除调入单失败' + resData['Data'][0]['ErrorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['ID']
        data['no'] = resData['Data'][0]['Code']
        return data, None

    # 调入单提交跟审核
    @classmethod
    def diaoRuTiJiaoShenHe(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 调入单提交
        url = cls.apiAdd + "TransferIn/Submit"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '调入单提交接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '调入单提交失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '调入单提交失败' + resData['Data'][0]['ErrorMsg']

        # 调入单审核
        url = cls.apiAdd + "TransferIn/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '调入单审核接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '调入单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '调入单审核失败' + resData['Data'][0]['ErrorMsg']
        return data, None

    @classmethod
    def prdtUpdate(cls,data):
        url = cls.apiAdd + "ItemMaster/Modify"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '料品修改接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '料品修改失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '料品修改失败' + resData['Data'][0]['m_errorMsg']
        return data, None
    @classmethod
    def deptUpdate(cls, data):
        # 更新部门
        url = cls.apiAdd + "Department/Modify"
        jsonData = data
        token, err = cls.getToken()
        if err != None:
            return None, err
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '部门修改接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '部门修改失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '部门修改失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    # 材料出库单新增
    @classmethod
    def clCk(cls, data):
        # 调入单提交
        url = cls.apiAdd + "MaterialDeliveryDoc/Create"
        jsonData = data
        print(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        token ,err = cls.getToken()
        if err!=None:
            return None,err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '材料出库单请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '材料出库单新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '材料出库单新增失败' + resData['Data'][0]['m_errorMsg']
        # 审核单据
        url = cls.apiAdd + "MaterialDeliveryDoc/Approve"
        shData = [
            {
                "ID": resData['Data'][0]["m_iD"],
                "DocNo": resData['Data'][0]["m_code"],
                # "CurrentSysVersion": 1,
                "isAutoApp": True,
                }
        ]
        r = requests.post(url, json=shData, headers=header)
        if r.status_code != 200:
            return None, '材料出库单审核请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '材料出库单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '材料出库单审核失败' + resData['Data'][0]['m_errorMsg']
        return resData, None

    @classmethod
    def clRk(cls, data):
        # 调入单提交
        url = cls.apiAdd + "MaterialDeliveryDoc/Create"
        jsonData = data
        print(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '材料出库单请求失败'
        resData = r.json()
        if resData['Success'] != True:
            return None, '材料入库单新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '材料入库单新增失败' + resData['Data'][0]['m_errorMsg']
        # 审核单据
        url = cls.apiAdd + "MaterialDeliveryDoc/Approve"
        shData = [
            {
                "ID": resData['Data'][0]["m_iD"],
                "DocNo": resData['Data'][0]["m_code"],
                # "CurrentSysVersion": 1,
                "isAutoApp": True,
            }
        ]
        r = requests.post(url, json=shData, headers=header)
        if r.status_code != 200:
            return None, '材料入库单审核请求失败'
        resData = r.json()

        # print(resData)
        if resData['Success'] != True:
            return None, '材料入库单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '材料入库单审核失败' + resData['Data'][0]['m_errorMsg']
        return resData, None

    @classmethod
    def zsSh(cls, code):
        # 审核单据
        url = cls.apiAdd + "MiscRcvTrans/Approve"
        shData = [
            {
                "Code": code
            }
        ]
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=shData, headers=header)
        if r.status_code != 200:
            return None, '杂收单审核请求失败'
        resData = r.json()

        # print(resData)
        if resData['Success'] != True:
            return None, '杂收单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '杂收单审核失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def zfSh(cls, code):
        # 审核单据
        url = cls.apiAdd + "MiscShip/Approve"
        shData = [
            {
                "Code": code
            }
        ]
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=shData, headers=header)
        if r.status_code != 200:
            return None, '杂发单审核请求失败'
        resData = r.json()

        # print(resData)
        if resData['Success'] != True:
            return None, '杂发单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '杂发单审核失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def addZsSh(cls, data):
        # 新增杂收单且审核
        url = cls.apiAdd + "MiscRcvTrans/Create"
        jsonData = data
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增杂收单请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '杂收单新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '杂收单新增失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def addZfSh(cls, data):
        # 新增杂发单且审核
        url = cls.apiAdd + "MiscShip/Create"
        jsonData = data
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增杂发单请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '杂发单新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '杂发单新增失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def addPoTh(cls, data):
        # 进货退回单，来源单是申请单
        url = cls.apiAdd + "Receivement/CreateRcvBySrc"
        jsonData = data
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增退回单请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '退货单新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '退货单新增失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def addSaCk(cls, data):
            # 调入单提交
            url = cls.apiAdd + "Ship/CreateBySrcDoc"
            jsonData = data
            print(jsonData)
            # print(json.dumps(jsonData,ensure_ascii=False))
            token, err = cls.getToken()
            if err != None:
                return None, err
            header = {
                "Content-Type": "application/json",
                "token": token
            }
            # 新增单据
            r = requests.post(url, json=jsonData, headers=header)
            if r.status_code != 200:
                return None, '标准出货单请求失败'
            resData = r.json()
            if resData['Success'] != True:
                return None, '标准出货单新增失败' + resData['ResMsg']
            if resData['Success'] == True:
                if resData['Data'][0]['m_isSucess'] == False:
                    return None, '标准出货单新增失败' + resData['Data'][0]['m_errorMsg']
            # 审核单据
            url = cls.apiAdd + "Ship/SubmitAndApprove"
            shData = [
                {
                    "ID": resData['Data'][0]["m_iD"],
                    "Code": resData['Data'][0]["m_code"]
                }
            ]
            r = requests.post(url, json=shData, headers=header)
            if r.status_code != 200:
                return None, '标准收货单提交并审核请求失败'
            resData = r.json()

            # print(resData)
            if resData['Success'] != True:
                return None, '标准收货单提交并审核失败' + resData['ResMsg']
            if resData['Success'] == True:
                if resData['Data'][0]['m_isSucess'] == False:
                    return None, '标准收货单提交并审核失败' + resData['Data'][0]['m_errorMsg']
            return resData, None

    @classmethod
    def addSaTh(cls, data):
            # 调入单提交
            url = cls.apiAdd + "Receivement/CreateSaleRcvBySrc"
            jsonData = data
            # print(json.dumps(jsonData,ensure_ascii=False))
            token, err = cls.getToken()
            if err != None:
                return None, err
            header = {
                "Content-Type": "application/json",
                "token": token
            }
            # 新增单据
            r = requests.post(url, json=jsonData, headers=header)
            if r.status_code != 200:
                return None, '销售退回收单请求失败'
            resData = r.json()
            if resData['Success'] != True:
                return None, '销售退回收单新增失败' + resData['ResMsg']
            if resData['Success'] == True:
                if resData['Data'][0]['IsSucess'] == False:
                    return None, '销售退回收单新增失败' + resData['Data'][0]['ErrorMsg']
            return resData, None

    # 标准收货行提交跟审核
    @classmethod
    def bzshRowSh(cls, data):
        # 标准收货行提交审核
        url = cls.apiAdd + "Receivement/SubmitLine"
        jsonData =data
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 提交审核行
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '标准收货单提交接口请求失败'
        resData = r.json()
        if resData['Success'] != True:
            return None, '标准收货单提交失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '标准收货单提交失败' + resData['Data'][0]['ErrorMsg']

        # 审核行
        url = cls.apiAdd + "Receivement/ApproveLine"
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '标准收货单审核行接口请求失败'
        resData = r.json()
        if resData['Success'] != True:
            return None, '标准收货单审核行失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '标准收货单审核行失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    # 成品入库单审核
    def diaoRUShenHe(cls, code):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 成品入库单审核
        url = cls.apiAdd + "TransferIn/Approve"
        jsonData = [
            {
            "OtherID": "",
            "ID": 0,
            "Code": code,
            "HoldOrReleaseReasonCode": "",
            "CancelResean": ""
            }
        ]
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '调入单审核接口请求失败'
        resData = r.json()
        if resData['Success'] != True:
            return None, '调入单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '调入单审核失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    @classmethod
    def tWRkSh(cls, code):
        # 审核单据
        url = cls.apiAdd + "TransferForm/Approve"
        shData = [
            {
                "Code": code
            }
        ]
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=shData, headers=header)
        if r.status_code != 200:
            return None, '托工入库形态转换单审核请求失败'
        resData = r.json()

        # print(resData)
        if resData['Success'] != True:
            return None, '托工入库形态转换单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '托工入库形态转换单审核失败' + resData['Data'][0]['ErrorMsg']
        return resData, None

    # 新增盘盈亏单
    @classmethod
    def addPYingKui(cls, data):
        # 新增杂发单且审核
        url = cls.apiAdd + "CheckDiffBill/Create"
        jsonData = data
        # print(json.dumps(jsonData,ensure_ascii=False))
        token, err = cls.getToken()
        if err != None:
            return None, err
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        # 新增单据
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增盘盈亏单请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '盘盈亏新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '盘盈亏新增失败' + resData['Data'][0]['ErrorMsg']
        return resData, None
# data = [
#   {
#     "Org": {
#       "Code": "100",
#       "Name": ""
#     },
#     "Code": "0302",
#     "DescFlexField": {
#       "PubDescSeg50": "22222123213412"
#     },
#   }
# ]
# token = """eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbnRlcnByaXNlSUQiOiIwMSIsIkVudENvZGUiOiIwMSIsIkN1bHR1cmVOYW1lIjoiemgtQ04iLCJPcmdDb2RlIjoiMTAwIiwiT3JnSUQiOiIxMDAyMzA1MTczNzI2MDAwIiwiT3JnTmFtZSI6IjEwMDI0MDkxMDAwMDA3MDciLCJVc2VySUQiOiIxMDAyNDA5MTAwMDAwNzA3IiwiVXNlckNvZGUiOiJ3bXMiLCJVc2VyTmFtZSI6IldNUyIsIkV4cGlyZSI6IjIwMjQtMDktMzBUMTI6NTc6MzMuODUzMjc1MyswODowMCIsIlZhbGlkQXVkaWVuY2UiOiIwMTEwMHdtczE3Mjc2ODY2NTM4NTMiLCJFZmZlY3RpdmVNaW4iOjI0MCwiQXBwSUQiOiJXTVMiLCJUb2tlbkV4dGVuZEtleSI6Indtc0AxMDBAMDFAV01TIn0.Jo0QX2NA4VTupyPuw32narcR3DuY07Y5aTJK-FjXRrE"""
# u9cApiList.deptUpdate(data,token)

# a = nowData()
# b = nowData(second=-1)
# print(a)
# print(b)